package ru.myrtle.umf.tools.ugen.templates;

import java.io.*;
import java.text.*;
import java.util.*;
import ru.myrtle.umf.*;
import ru.myrtle.umf.codegen.*;
import ru.myrtle.umf.codegen.java.*;
import ru.myrtle.umf.tools.ugen.*;

public class UObjectOpTemplate extends UTemplate
{
    protected final String NL = System.getProperties().getProperty("line.separator");
    protected final String TEXT_1 = "package ";
    protected final String TEXT_2 = ";" + NL + NL + "public class ";
    protected final String TEXT_3 = " extends ";
    protected final String TEXT_4 = " ";
    protected final String TEXT_5 = " {" + NL;
    protected final String TEXT_6 = NL + "}" + NL;

    /*
     *
     */


    public UObjectOpTemplate() {

    }

    /*
     *
     */

    public String generate(UTemplateContext context) throws UTemplateRejectException
    {
        final StringBuffer stringBuffer = new StringBuffer();

    checkTemplateObjectType(context, UClass.class);

    UClass object = (UClass) context.getObject();

    if (!UGenUtil.hasDerivedFeatures(object) && !UGenUtil.hasNotProvidedOperations(object)) {
        reject();
    }

    String opClassName     = UGenUtil.getObjectOpClassName(object);
    String opClassPackage  = UGenUtil.getObjectOpClassPackage(object);

    String opStubClassName    = UGenUtil.getObjectOpStubClassName(object);
    String opStubClassPackage = UGenUtil.getObjectOpStubClassPackage(object);

    context.getResource().setObjectFileName(opClassName);
    context.getResource().setObjectFileExtension("java");
    context.getResource().setObjectPackage(opClassPackage);

    JavaImportBuilder importBuilder = new JavaImportBuilder();

    importBuilder.addPackage("java.util");
    importBuilder.addPackage("ru.myrtle.umf");
    importBuilder.addPackage("ru.myrtle.umf.util");
    importBuilder.addPackage(opStubClassPackage);

    String classBody = NL;
    String implementsSection = "";

    JavaFile javaFile = new JavaFile();
    javaFile.decompose(context.getResource());

    classBody = javaFile.getCode();
    implementsSection = javaFile.getImplementsSection();

    for (String importLine : javaFile.getImports()) {
        importBuilder.addObjectPackage(importLine);
    }

    classBody += NL;

        stringBuffer.append(TEXT_1);
        stringBuffer.append(opClassPackage);
        stringBuffer.append(TEXT_2);
        stringBuffer.append(opClassName);
        stringBuffer.append(TEXT_3);
        stringBuffer.append(opStubClassName);
        stringBuffer.append(TEXT_4);
        stringBuffer.append(implementsSection);
        stringBuffer.append(TEXT_5);
        stringBuffer.append(classBody);
        stringBuffer.append(TEXT_6);

    insertImport(stringBuffer, importBuilder, opClassPackage);


        return stringBuffer.toString();
    }

    // ---------------------------------------------------------------------------------------
    // ---------------------------------------------------------------------------------------

}
